var express = require('express');
var router = express.Router();
var EventProxy = require('eventproxy');
var util = require('util');

var logger = require("../models/logger")
var Course = require("../models/Course");
var Focus = require("../models/Focus");
var resultJson = require("../result-json");
var Utils = require("../utils");
var Consts = require("../consts");

router.post('/', function(req, res) {
    res.send('respond with a resource');
});

//焦点图管理(根据index进行维护)
router.post('/manageFocus', function(req, res) {
    var paramFocus = req.body;
    var proxy = new EventProxy();

    Focus.findOne({
        index: paramFocus.index
    }).exec(function(err, focus) {
        if (err)
            res.send(resultJson(0));
        if (!focus){
            new Focus(paramFocus).save(function(err, findFocus) {
                if (err) {
                    logger.error(err);
                    return res.send(resultJson(2));
                }
                res.send(resultJson(0, findFocus));
            });
        }else{
            Focus.update({
                _id: focus._id
            }, {
                $set: paramFocus
            }).exec(function(err, findFocus) {
                if (err) {
                    logger.error(err);
                    return res.send(resultJson(2));
                }
                res.send(resultJson(0, findFocus));
            });
        }

        res.send(resultJson(0, focus));
    });
});

//首页焦点图列表
router.post('/listFocus', function(req, res) {
    var focus = req.body;
    var proxy = new EventProxy();
    proxy.all("results", "count", function(results, count) {
        res.send(resultJson(0, {
            results: results,
            count: count
        }))
    })
    var option = {};
    Focus.find(option).sort({
        index: 1
    }).exec(function(err, focuses) {
        if (err) {
            logger.error(err);
            return res.send(resultJson(2));
        }
        proxy.emit("results", focuses);
    });
    Focus.find(option).count(function(err, num) {
        if (err) {
            logger.error(err);
            return res.send(resultJson(2));
        }
        proxy.emit("count", num);
    });
});

//增加新课程或修改课程
router.post('/upsert', function(req, res) {
    var course = req.body;
    course.create = new Date();
    var _id = course._id;
    if(!course.chapter)
        course.chapter = [];
    if (course._id) {
        delete course._id;
        Course.update({
            _id: _id
        }, {
            $set: course
        }).exec(function(err) {
            if (err) {
                logger.error(err);
                return res.send(resultJson(2));
            }
            res.send(resultJson(0));
        });
    } else {
        new Course(course).save(function(err, course) {
            if (err) {
                logger.error(err);
                return res.send(resultJson(2));
            }
            res.send(resultJson(0, course));
        });
    }
});

//列出所有课程
router.post('/list', function(req, res) {
    var course = req.body;
    var proxy = new EventProxy();
    proxy.all("results", "count", function(results, count) {
        res.send(resultJson(0, {
            results: results,
            count: count
        }))
    })
    var start = course.start || 0;
    var count = course.count || Consts.MAX;
    var option = {};
    Course.find(option).skip(start).limit(count).sort({
        create: -1
    }).exec(function(err, courses) {
        if (err) {
            logger.error(err);
            return res.send(resultJson(2));
        }
        proxy.emit("results", courses);
    });
    Course.find(option).count(function(err, num) {
        if (err) {
            logger.error(err);
            return res.send(resultJson(2));
        }
        proxy.emit("count", num);
    });
});

//删除
router.post('/remove', function(req, res) {
    var course = req.body;
    Course.remove({
        _id: course._id
    }, function(err) {
        if (err) {
            logger.error(err);
            res.send(resultJson(2));
        } else
            res.send(resultJson(0));
    });
});

//根据课程id查询课程
router.post("/detail", function(req, res) {
    var course = req.body;
    if (!course._id)
        return res.send(resultJson(3));

    Course.findById(course._id).exec(function(err, course) {
        if (err) {
            logger.error(err);
            return res.send(resultJson(2));
        }
        
        return res.send(resultJson(0, course));
    })
});

module.exports = router;